﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using AjaxControlToolkit;
using DTO;
using BUS;

namespace WebRaoVat
{
    public partial class UpTinTuDong : System.Web.UI.Page
    {
        TinRaoVatDTO tinRaoVat = new TinRaoVatDTO();
        public NguoiDungDTO nguoiDung = new NguoiDungDTO();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                nguoiDung = NguoiDungBUS.layNguoiDungTheoTen(Session["NguoiDungDangDangNhap"].ToString());
                //nguoiDung = NguoiDungBUS.layNguoiDungTheoTen("N_M_T174");
            }
            catch (System.Exception)
            {
                nguoiDung = null;
                return;
            }

            if (nguoiDung != null && nguoiDung.MaLoaiNguoiDung != 1)
            {
                tinRaoVat = TinRaoVatBUS.getAlongCode_MaTinRaoVat(int.Parse(Session["MaTinRaoVatDuocChon"].ToString()));
            }

            if (!IsPostBack)
            {
                
                txtTenTin.Text = tinRaoVat.TenTinRaoVat;

                txtDate.Text = DateTime.Today.Month.ToString() + "/" + DateTime.Today.Day.ToString() + "/"
                    + DateTime.Today.Year.ToString();
                for (int i = 0; i < 24; i++)
                {
                    DropDownListHour.Items.Add(i.ToString("00"));
                    DropDownListGio.Items.Add(i.ToString("00"));
                }
                for (int i = 0; i < 60; i++)
                {
                    DropDownListMinu.Items.Add(i.ToString("00"));
                    DropDownListPhut.Items.Add(i.ToString("00"));
                    if(i>=15)
                        DropDownListGiay.Items.Add(i.ToString("00"));
                }
            }

        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            Page.Theme = ((XmlElement)XL_XML.ReadNodeSingle(XL_XML.ReadDocumentFile(Server.MapPath("Gui.xml")),
            "/GIAODIEN/THEME")).GetAttribute("ten");
        }

        protected void btnDongYUp_Click(object sender, EventArgs e)
        {
            int maTinRaoVat = tinRaoVat.MaTinRaoVat;
         
            string time = txtDate.Text + " ";
            time += DropDownListHour.Text;
            time += ":";
            time += DropDownListMinu.Text;
            time += ":00";
            DateTime thoiGianBatDau = DateTime.Parse(time);

            DateTime thoiGianKetThuc = thoiGianBatDau.AddDays(7);

            double khoangThoiGianTuDongUp = (int.Parse(DropDownListGio.Text) * 60 * 60 +
                int.Parse(DropDownListPhut.Text) * 60 +
                int.Parse(DropDownListGiay.Text)) * 1000;

            UpTinDTO upTin = new UpTinDTO();
            upTin.MaTinRaoVat = maTinRaoVat;
            upTin.KhoangThoiGianTuDongUp = khoangThoiGianTuDongUp;
            upTin.ThoiGianBatDau = thoiGianBatDau;
            upTin.ThoiGianKetThuc = thoiGianKetThuc;

            if (UptinBUS.Insert(upTin))
            {
                TaskScheduler _scheduler = new TaskScheduler(upTin.KhoangThoiGianTuDongUp,
                        upTin.MaTinRaoVat, upTin.ThoiGianBatDau, upTin.ThoiGianKetThuc);
                _scheduler.StartTask();

                //showw thành công
            }
            Response.Redirect("NguoiDung.aspx");
        }
    }
}
